<?php

namespace wework;

use app\admin\model\UserPvLog;
use app\sys\model\QyeSendLog;
use CorpAPI;
use Department;
use Message;
use MkMessageContent;
use MpNewsArticle;
use MpNewsMessageContent;
use think\facade\Log;


class WeWork
{
    private $api;
    public $info;
    public $users;
    public $agentId;
    public $staffNo;
    public $userNo;
    public $storeId;
    public $qyeToUser = array();
    public $all;

    public function __construct($corpId,$secret)
    {
        $this->api = new CorpAPI($corpId, $secret);
    }

    //
    public function sendMessage()
    {
        $contentStr = $this->info;

        try {
            //
            $message = new Message();
            {
                if($this->all){
//                    $message->sendToAll = true;
                    $message->touser = $this->all;
                }else{
                    $message->touser = $this->qyeToUser;
                }
                $message->agentid = 1000002;
                $message->safe = 0;

                $message->messageContent = new MkMessageContent(
                    $content = $contentStr
                );
            }
            $invalidUserIdList = null;
            $invalidPartyIdList = null;
            $invalidTagIdList = null;

            // 记录添加
            $res = QyeSendLog::addSendLog($this->staffNo,$this->userNo);

            if($res != "false"){

                Log::info("QyeMessageSend".json_encode($message));
                $this->api->MessageSend($message, $invalidUserIdList, $invalidPartyIdList, $invalidTagIdList);
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return "";
    }

    public function setMessage($userArr,$all,$name,$content,$url,$agent_id="",$staff_no="",$user_no="",$store_id=0)
    {

        $this->all = $all;
        $this->qyeToUser = $userArr;
        $this->agentId = $agent_id;
        $this->staffNo = $staff_no;
        $this->userNo = $user_no;
        $this->storeId = $store_id;

        if($this->all){
            $this->getTis($url);
        }else{
            $this->getInfo($name,$content,$url);
        }

    }

    public function getInfo($name,$content,$url)
    {
        // 店铺信息
        if(!$content){
            $logFind = UserPvLog::field("title")->where("pxs_id",$this->storeId)->group("id desc")->find();
            if($logFind) $content = "店铺信息".$logFind["title"];
        }
        $date = date("Y-m-d H:i:s");
        $this->info = "智能客服系统实时咨询消息
                    \n>**事项详情**
                     > 
                     >用户:<font color=\"warning\">{$name}</font>
                     >咨询时间:<font color=\"info\">{$date}</font>  
                     >咨询内容:<font color=\"info\">{$content}</font> 
                     > 
                     >请及时回复~ 
                     > 
                     >请用手机点击跳转,电脑暂不支持:[点击详情]({$url})"
        ;
    }

    public function getTis($url)
    {
        $this->info = "绑定链接消息
                    \n>**事项详情**
                     > 
                     >绑定企业工号（格式为：PXS000001）以便接收离线消息,用户注册
                     >请大家配合~
                     > 
                     >请用手机点击跳转,电脑暂不支持:[点击详情]({$url})"
        ;
    }

    // 获取部门
    public function departmentList()
    {
        try {
            $departmentList = $this->api->DepartmentList();
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return $departmentList;
    }

    public function userSimpleList($department_id)
    {
        try {
            $departmentList = $this->api->UserSimpleList($department_id,0);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return $departmentList;
    }


    // code
    public function getUserInfoByCode($code):array
    {
        $UserInfoByCode = $this->api->GetUserInfoByCode($code);
        if(!$UserInfoByCode){
            return ["err","企业微信链接错误~"];
        }

        if(!isset($UserInfoByCode->UserId)){
            return ["err","还不是企业人员哦~，请联系人事"];
        }
        return [$UserInfoByCode->UserId,$UserInfoByCode->DeviceId];
    }
}